import type { APIEndpoint } from '../types'

/**
 * 用户
 */
export interface GreasyForkOfficialUser {
  id: number
  name: string
  created_at: string
  url: string
  scripts: Omit<GreasyForkOfficialScript, 'users'>[]
}

/**
 * 脚本
 */
export interface GreasyForkOfficialScript {
  id: number
  daily_installs: number
  total_installs: number
  fan_score: string
  good_ratings: number
  ok_ratings: number
  bad_ratings: number
  created_at: string
  code_updated_at: string
  namespace: string
  support_url: string | null
  contribution_url: string | null
  contribution_amount: string | null
  users: Omit<GreasyForkOfficialUser, 'scripts'>[]
  name: string
  description: string
  url: string
  code_url: string
  code_size: number
  license: string
  version: string
  locale: string
  deleted: boolean
}

/**
 * 脚本版本历史
 */
export type GreasyForkOfficialScriptVersions = GreasyForkOfficialScriptVersion[]
export interface GreasyForkOfficialScriptVersion {
  version: string
  created_at: string
  url: string
  code_url: string
}

/**
 * API 端点枚举
 */
export enum GreasyForkAPIEndpoint {
  SCRIPT = 'script',
  SCRIPT_VERSIONS = 'scriptVersions',
  USER = 'user',
  USER_SCRIPTS = 'userScripts',
}

/**
 * API 端点配置映射
 */
export const GREASY_FORK_API_ENDPOINTS: Record<GreasyForkAPIEndpoint, APIEndpoint> = {
  [GreasyForkAPIEndpoint.SCRIPT]: {
    path: '/scripts/{id}.json',
    description: '获取脚本信息',
  },
  [GreasyForkAPIEndpoint.SCRIPT_VERSIONS]: {
    path: '/scripts/{id}/versions.json',
    description: '获取脚本版本历史',
    defaultRetryCount: 3,
  },
  [GreasyForkAPIEndpoint.USER]: {
    path: '/users/{id}.json',
    description: '获取用户信息',
  },
  [GreasyForkAPIEndpoint.USER_SCRIPTS]: {
    path: '/users/{id}.json',
    description: '获取用户的所有脚本',
  },
}
